<?php 
//this file handles all the jazz for forum admin

function addForum($name, $description, $readMode, $position, $categoryID, $groupID, $moderatorGroupID){
	global $link;
	
	//clean up the incoming vars
	$name = mysql_real_escape_string($name);
	$description = mysql_real_escape_string($description);
	$readMode = mysql_real_escape_string($readMode);
	$position = mysql_real_escape_string($position);
	$categoryID = mysql_real_escape_string($categoryID);
	$groupID = mysql_real_escape_string($groupID);
	$moderatorGroupID = mysql_real_escape_string($moderatorGroupID);
	
	//update the database and push all the forums to make room for the new one
	$result = mysql_query("UPDATE ".TABLE_PREFIX."_forums 
						   SET displayOrder = displayOrder + 1 
						   WHERE categoryID = ".$categoryID." 
						   AND displayOrder > ".$position);
	if(!$result){
		echo "output=mySqlError&error=".mysql_error();
		return;
	}
	
	//do a quick forum read mode check
	if($readMode == ""){
		$readMode = 0;
	}
	
	//insert the new forum
	$result = mysql_query("INSERT INTO ".TABLE_PREFIX."_forums (categoryID, groupID, moderatorGroupID, title, description, readMode, displayOrder) 
						   VALUES (".$categoryID.", ".$groupID.", ".$moderatorGroupID.", '".$name."', '".$description."', ".$readMode.", ".($position + 1).")");
	if(!$result){
		echo "output=mySqlError&error=".mysql_error();
		return;
	}
	
	//report success
	return "output=success&newForumID=".mysql_insert_id();
}

function deleteForum($categoryID, $forumID, $deleteThreads){
	global $link;
	
	//clean up incoming
	$categoryID = mysql_real_escape_string($categoryID);
	$forumID = mysql_real_escape_string($forumID);
	$deleteThreads = mysql_real_escape_string($deleteThreads);
	
	//update the other display orders
	$result = mysql_query("SELECT displayOrder FROM ".TABLE_PREFIX."_forums WHERE forumID = ".$forumID);
	$data = mysql_fetch_object($result);
	$displayOrder = $data->displayOrder;
	
	$result = mysql_query("UPDATE ".TABLE_PREFIX."_forums SET displayOrder = displayOrder - 1 
						   WHERE categoryID = ".$categoryID." AND displayOrder > ".$displayOrder);
	if(!$result){
		echo "output=mySqlError&error=".mysql_error();
		return;
	}
	
	//handle the delete
	$result = mysql_query("DELETE FROM ".TABLE_PREFIX."_forums WHERE forumID = ".$forumID);
	
	if(!$result){
		echo "output=mySqlError&error=".mysql_error();
		return;
	}
	
	//now if they want to wipe out everything under that forum as well, handle that
	if($deleteThreads == "true"){
		$result = mysql_query("DELETE t.*, p.*
							   FROM ".TABLE_PREFIX."_threads t, ".TABLE_PREFIX."_posts p
							   WHERE t.forumID = ".$forumID."
							   AND p.threadID = t.threadID");
		if(!$result){
			echo "output=mySqlError&error=".mysql_error();
			return;
		}
	}
	
	//we made it!!
	return "output=success";
}
?>